﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public class TopicBarRenderer
    {
        public TopicBarRenderer(TopicBar TopicBar)
        {
            this.m_TopicBar = TopicBar;
        }

        private TopicBar m_TopicBar;

        public TopicBar TopicBar
        {
            get
            {
                return this.m_TopicBar;
            }
        }

        private StringFormat StringFormat
        {
            get
            {
                var Format = new StringFormat();
                Format.LineAlignment = StringAlignment.Center;
                return Format;
            }
        }

        public virtual void OnRenderBackground(PaintEventArgs e)
        {
        }

        public virtual void OnRenderPageBackground(TopicBarPageRendererEventArgs e)
        {
            // 绘制渐变矩形==============================================
            using (var Brushes = new LinearGradientBrush(e.ClipRectangle, this.TopicBar.PageTopColor, this.TopicBar.PageBottomColor, 90f))
            {
                if (this.TopicBar.SelectedRectangle != default && e.Page.Rectangle == this.TopicBar.SelectedRectangle)
                {
                    Brushes.LinearColors = new[] { this.TopicBar.MouseMovePageTopColor, this.TopicBar.MouseMovePageBottomColor };
                }

                e.Graphics.FillRectangle(Brushes, e.ClipRectangle);
            }
            // 绘制分割线================================================
            using (var Pen = new Pen(this.TopicBar.PageSplit0Color))
            {
                e.Graphics.DrawLine(Pen, new Point(e.ClipRectangle.X, e.ClipRectangle.Top), new Point(e.ClipRectangle.Width - 1, e.ClipRectangle.Top));
                Pen.Color = this.TopicBar.PageSplit1Color;
                e.Graphics.DrawLine(Pen, new Point(e.ClipRectangle.X, e.ClipRectangle.Top), new Point(e.ClipRectangle.Width - 1, e.ClipRectangle.Top));
                Pen.Color = this.TopicBar.PageSplit2Color;
                e.Graphics.DrawLine(Pen, new Point(e.ClipRectangle.X, e.ClipRectangle.Bottom), new Point(e.ClipRectangle.Width - 1, e.ClipRectangle.Bottom));
            }
            // 绘制下拉箭头==============================================
            Image arrow = Properties.Resources.TopicBar_arrow0;
            if (e.Page.ExpandPage)
            {
                arrow = Properties.Resources.TopicBar_arrow1;
            }

            int arrow_x = (int)Math.Round(e.ClipRectangle.Right - arrow.Height - (e.ClipRectangle.Height - arrow.Height) / 2d);
            int arrow_y = (int)Math.Round(e.ClipRectangle.Top + (e.ClipRectangle.Height - arrow.Height) / 2d);
            e.Graphics.DrawImage(arrow, arrow_x, arrow_y);
        }

        public virtual void OnRenderPageImage(TopicBarPageRendererEventArgs e)
        {
            // 绘制图标==================================================
            if (e.Page.Image is object)
            {
                var myCallback = new Image.GetThumbnailImageAbort(this.TopicBar.ThumbnailCallback);
                Image PageImage;
                int PageImage_x;
                int PageImage_y;
                PageImage = e.Page.Image.GetThumbnailImage(this.TopicBar.IconSize.Width, this.TopicBar.IconSize.Height, myCallback, IntPtr.Zero);
                PageImage_x = (int)Math.Round((e.ClipRectangle.Height - PageImage.Height) / 2d);
                PageImage_y = e.ClipRectangle.Top + PageImage_x;
                e.Graphics.DrawImage(PageImage, 8, PageImage_y);
            }
        }

        public virtual void OnRenderPageText(TopicBarPageRendererEventArgs e)
        {
            // 绘制标题==================================================
            if (!string.IsNullOrEmpty(e.Page.Text))
            {
                using (var Brush = new SolidBrush(this.TopicBar.PageForeColor))
                {
                    e.Graphics.DrawString(e.Page.Text, this.TopicBar.PageForeFont, Brush, new RectangleF(8 + this.TopicBar.IconSize.Width + 6, 0f, e.ClipRectangle.Width, this.TopicBar.PageHeight), this.StringFormat);
                }
            }
        }

        public virtual void OnRenderItemBackground(TopicBarItemRendererEventArgs e)
        {
            using (var Brushes = new LinearGradientBrush(e.ClipRectangle, this.TopicBar.ItemTopColor, this.TopicBar.ItemBottomColor, 90f))
            {
                if (ReferenceEquals(e.Item, this.TopicBar.SelectedItem))
                {
                    Brushes.LinearColors = new[] { this.TopicBar.SelectedItemTopColor, this.TopicBar.SelectedItemBottomColor };
                }
                else if (e.Item.Rectangle == this.TopicBar.SelectedRectangle)
                {
                    Brushes.LinearColors = new[] { this.TopicBar.MouseMoveItemTopColor, this.TopicBar.MouseMoveItemBottomColor };
                }

                e.Graphics.FillRectangle(Brushes, e.ClipRectangle);
            }
        }

        public virtual void OnRenderItemImage(TopicBarItemRendererEventArgs e)
        {
            // 绘制图标==================================================
            if (e.Item.Image is object)
            {
                var myCallback = new Image.GetThumbnailImageAbort(this.TopicBar.ThumbnailCallback);
                Image PageImage;
                int PageImage_x;
                int PageImage_y;
                PageImage = e.Item.Image.GetThumbnailImage(16, 16, myCallback, IntPtr.Zero);
                PageImage_x = (int)Math.Round((e.ClipRectangle.Height - PageImage.Height) / 2d);
                PageImage_y = e.ClipRectangle.Top + PageImage_x;
                e.Graphics.DrawImage(PageImage, 8, PageImage_y);
            }
        }

        public virtual void OnRenderItemText(TopicBarItemRendererEventArgs e)
        {
            // 绘制标题==================================================
            if (!string.IsNullOrEmpty(e.Item.Text))
            {
                var Item_Font = this.TopicBar.ItemForeFont;
                var Item_ForeColor = this.TopicBar.ItemForeColor;
                if (ReferenceEquals(e.Item, this.TopicBar.SelectedItem))
                {
                    Item_Font = this.TopicBar.SelectedItemFont;
                    Item_ForeColor = this.TopicBar.SelectedItemForeColor;
                }
                else if (e.Item.Rectangle == this.TopicBar.SelectedRectangle)
                {
                    Item_Font = this.TopicBar.MouseMoveItemFont;
                    Item_ForeColor = this.TopicBar.MouseMoveItemForeColor;
                }

                using (var Brush = new SolidBrush(Item_ForeColor))
                {
                    if (this.TopicBar.DrawMode == TopicBar.DarwModeEnum.Row)
                    {
                        e.Graphics.DrawString(e.Item.Text, Item_Font, Brush, new RectangleF(8 + this.TopicBar.IconSize.Width + 6, 0f, e.ClipRectangle.Width, this.TopicBar.ItemHeight), this.StringFormat);
                    }
                    else
                    {
                        var Format = new StringFormat();
                        Format.LineAlignment = StringAlignment.Center;
                        Format.Alignment = StringAlignment.Center;
                        e.Graphics.DrawString(e.Item.Text, Item_Font, Brush, new RectangleF(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height), Format);
                    }
                }
            }
        }
    }

    public class TopicBarPageRendererEventArgs
    {
        internal TopicBarPageRendererEventArgs()
        {
        }

        public TopicBar TopicBar
        {
            get
            {
                return this.m_TopicBar;
            }
        }

        internal TopicBar m_TopicBar;

        public Graphics Graphics
        {
            get
            {
                return this.m_Graphics;
            }
        }

        internal Graphics m_Graphics;

        public Rectangle ClipRectangle
        {
            get
            {
                return new Rectangle(0, 0, this.Page.Rectangle.Width, this.Page.Rectangle.Height);
            }
        }

        public TopicBarPage Page
        {
            get
            {
                return this.m_Page;
            }
        }

        internal TopicBarPage m_Page;
    }

    public class TopicBarItemRendererEventArgs
    {
        internal TopicBarItemRendererEventArgs()
        {
        }

        public TopicBar TopicBar
        {
            get
            {
                return this.m_TopicBar;
            }
        }

        internal TopicBar m_TopicBar;

        public Graphics Graphics
        {
            get
            {
                return this.m_Graphics;
            }
        }

        internal Graphics m_Graphics;

        public Rectangle ClipRectangle
        {
            get
            {
                return new Rectangle(0, 0, this.Item.Rectangle.Width, this.Item.Rectangle.Height);
            }
        }

        public TopicBarItem Item
        {
            get
            {
                return this.m_Item;
            }
        }

        internal TopicBarItem m_Item;
    }
}